const app = getApp()

import Notify from '@vant/weapp/notify/notify';


import {
    callScanUrl
} from "../../api/common/scan.js"

import {
    userProfile,
    getUserInfoRq
} from "../../api/user/user.js"

import {
    getParkRq
} from "../../api/index/index.js"

import {
    getUrlParamsObj
} from "../../utils/util.js"

Page({
    data: {
        // tabBar param
        tabBarParam: {
            selected: 0,
            color: "#515151",
            selectedColor: "#217CFF",
            backgroundColor: "#ffffff",
        },
        // tabBar menu
        tabBarList: [{
                "pagePath": "/pages/index/newIndex/newIndex",
                "iconPath": "/images/tabbar/home.png",
                "selectedIconPath": "/images/tabbar/home-select.png",
                "text": "首页"
            },
            {
                "pagePath": "pages/meeting/meetingReservation/meetingReservation",
                "iconPath": "/images/tabbar/openDoor.png",
                "selectedIconPath": "/images/tabbar/openDoor.png",
                "text": "预约",
                "bulge": true,
            },
            {
                "pagePath": "pages/my/my",
                "iconPath": "/images/tabbar/my.png",
                "selectedIconPath": "/images/tabbar/my-select.png",
                "text": "我的"
            },
        ],
        IMG_NAME: app.IMG_NAME,
        menuList: [
            // {
            //     name: "创业活动",
            //     img: "/images/hicon/chuangyehuodong.png",
            //     path: "/pages/community/community"
            // },
            // {
            //     name: "政策信息",
            //     img: "/images/hicon/zhengcexinxi.png",
            //     path: "/pages/notice/notice"
            // },
            // {
            //     name: "党建园地",
            //     img: "/images/hicon/zhaozhengce.png",
            //     path: "/pages/party/party"
            // },
            // {
            //     name: "租办公室",
            //     img: "/images/hicon/zubangongshi.png",
            //     path: "/pages/office/office"
            // },
            // {
            //     name: "项目入孵",
            //     img: "/images/hicon/xiangmurufu.png",
            //     path: "/pages/applyType/applyType"
            // },
            // {
            //     name: "企业入驻",
            //     img: "/images/hicon/qiyeruzhu.png",
            //     path: "/pages/applyType/applyType"
            // },
            // {
            //     name: "厂房申请",
            //     img: "/images/hicon/qianrushenqing.png",
            //     path: "/pages/applyType/applyType"
            // },
            // {
            //     name: "园区建议",
            //     img: "/images/hicon/yuanqujianyi.png",
            //     path: "/pages/complaint/complaint"
            // },
            // {
            //     name: "商业计划书",
            //     img: "/images/hicon/wenjuandiaocha.png",
            //     path: "/pages/outer/outer"
            // },
            /*
            {
              name: "园区报修",
              img: "/images/hicon/zhaozhuanli.png",
              path: "/pages/parkRepair/parkRepair"
            },      
            */
            {
                name: "会议预约",
                img: "/profile/static/index/menu-hyyy.png",
                path: "/pages/meeting/meetingReservation/meetingReservation"
            },
            // {
            //     name: "展厅预约",
            //     img: "/profile/static/index/menu-ztyy.png",
            //     path: "/pages/meeting/exhibition/list/list"
            // },
            // {
            //     name: "访客预约",
            //     img: "/profile/static/index/menu-fkyy.png",
            //     path: "/pages/meeting/visitorIinvitation/list/list"
            // },
            {
                name: "预约记录",
                img: "/profile/static/index/menu-yyjl.png",
                path: "/pages/meeting/reservationRecord/reservationRecord"
            },

            // {
            //     name: "远程门禁",
            //     img: "/profile/static/index/menu-ycmj.png",
            //     path: "/pages/meeting/accessControl/accessControl"
            // },
            // {
            //     name: "报修服务",
            //     img: "/profile/static/index/menu-bxfw.png",
            //     path: "/pages/parkRepair/parkRepair"
            // },
            // {
            //     name: "投诉建议",
            //     img: "/profile/static/index/menu-tsjy.png",
            //     path: "/pages/complaint/complaint"
            // },
            // {
            //     name: "社群活动",
            //     img: "/profile/static/index/menu-sqhd.png",
            //     path: "/pages/community/community"
            // },
            // {
            //     name: "政策中心",
            //     img: "/profile/static/index/menu-zczx.png",
            //     path: "/pages/notice/notice"
            // },
            // {
            //     name: "入住申请",
            //     img: "/profile/static/index/menu-rzsq.png",
            //     path: "/pages/applyType/applyType"
            // },

        ],
        bannerList: [],
        userDetail: {},
        parkName: '首页',
        qrcodeParam: null,
    },

    onLoad(options) {
        console.log('onLoad options', options);
        let _this = this;
        // 获取到二维码原始链接内容
        if (options.q) {
            let url = decodeURIComponent(options.q)
            let urlParam = getUrlParamsObj(url);
            // 自动绑定园区
            wx.setStorageSync('parkId', urlParam.parkId)
            wx.setStorageSync('parkName', urlParam.parkName)
            // 是否携带其他参数
            if (!urlParam.noParam) {
                _this.setData({
                    qrcodeParam: urlParam
                })
            }
        }
    },

    // 切换tabbar
    switchTab(e) {
        const data = e.currentTarget.dataset
        console.log('switchTab', data)
        const url = data.path
        if (url == 'pages/meeting/accessControl/accessControl') {
            wx.navigateTo({
                url: '/' + url
            })
        } else {
            wx.switchTab({
                url: '/' + url
            })
        }
    },

    onShow() {
        let _this = this
        let userId = wx.getStorageSync('userId');
        if (userId) {
            getUserInfoRq(userId).then(res => {
                console.log('getUserInfoRq', res);
                if (res.code == 0) {
                    let userDetail = res.data;
                    // 存储用户信息
                    wx.setStorageSync('openid', userDetail.openid)
                    wx.setStorageSync('user', userDetail)
                    wx.setStorageSync('userId', userDetail.id)
                    _this.setData({
                        userDetail
                    })
                    // 登录用户是否选择园区
                    _this.userIsPark()
                } else {
                    app.selfShowMsg(res.msg, '/pages/login/login')
                }
            })
        } else {
            app.selfShowMsg('请登录账号', '/pages/login/login')
        }
    },

    // 登录用户是否选择园区
    userIsPark() {
        let _this = this;
        let parkId = wx.getStorageSync('parkId');
        if (parkId) {
            _this.setData({
                parkName: wx.getStorageSync('parkName')
            })
            getParkRq(parkId).then(res => {
                console.log('getParkRq', res);
                let bannerList = res.bannerImages.map(item => item.url)
                _this.setData({
                    bannerList
                })
            })
            // 判断是否扫码
            if (_this.data.qrcodeParam) {
                // 扫码调用地址
                _this.scanCallUrl()
            }
        } else {
            wx.navigateTo({
                url: '/pages/index/parkList/parkList',
            })
        }
    },



    // 跳转园区
    jumpPark() {
        wx.navigateTo({
            url: '/pages/index/parkList/parkList',
        })
    },

    navapply(e) {
        if (wx.getStorageSync('token')) {
            if (e.currentTarget.dataset.path) {
                wx.navigateTo({
                    url: e.currentTarget.dataset.path + '?name=' + e.currentTarget.dataset.name,
                })
            } else {
                wx.showModal({
                    content: '模块暂未开放，敬请期待',
                    showCancel: false,
                })
            }
        } else {
            app.getlogin()
        }
    },


    // 扫一扫
    jumpScan() {
        let _this = this;
        wx.scanCode({
            scanType: 'qrCode',
            success(res) {
                console.log('success', res)
                let url = res.result;
                // 判断二维码是否符合规范
                if (url.indexOf(app.DOMAIN_NAME_PREFIX) != -1) {
                    let urlParam = getUrlParamsObj(url);
                    // 判断是否有设备参数
                    if (!urlParam.noParam) {
                        _this.setData({
                            qrcodeParam: urlParam
                        })
                        // 扫码调用地址
                        _this.scanCallUrl()
                    } else {
                        // 危险通知
                        _this.showErrMsg('请预约会议!')
                    }
                } else {
                    // 危险通知
                    _this.showErrMsg('二维码不正确!')
                }
            },
            fail(res) {
                console.log('fail', res)
                // 危险通知
                _this.showErrMsg('扫码失败!')
            }
        })
    },

    // 扫码调用地址
    scanCallUrl() {
        let _this = this;
        let qrcodeParam = _this.data.qrcodeParam;
        qrcodeParam.userId = wx.getStorageSync('userId')
        callScanUrl(qrcodeParam).then(res => {
            // 清空扫码数据
            _this.setData({
                qrcodeParam: null
            })
            if (res.code == 0) {
                // 成功通知
                _this.showSuccessMsg(res.msg)
            } else {
                // 危险通知
                _this.showErrMsg(res.msg)
            }
        })
    },

    // 成功通知
    showSuccessMsg(msg) {
        Notify({
            type: 'success',
            top: '160rpx',
            message: msg
        });
    },

    // 危险通知
    showErrMsg(msg) {
        Notify({
            type: 'danger',
            top: '160rpx',
            message: msg
        });
    },

})